package ie.lawlor.amvc.component.card.swing;

import ie.lawlor.amvc.event.Event;
import ie.lawlor.amvc.swing.JPanelView;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import javax.swing.JPanel;


/**
 * @author blawlor
 *
 */
public class CardView extends JPanelView {

  private Map panels= new HashMap(); 
  private CardLayout cardLayout = new CardLayout();
	public CardView(String name) {
        super(name);
        setLayout(cardLayout);
    }
    
    public void setCardNames(List cardNames){
    	for (ListIterator itr = cardNames.listIterator(); itr.hasNext();){
    		String cardName = (String)itr.next();
    		JPanel panel = new JPanel(new BorderLayout());
    		panels.put(cardName, panel);
    		add(panel, cardName);
    	}
    }
    
    public void doChangeCard(Event e){
      logger.debug("Switching to card " + e.getPayload());
      cardLayout.show(this, (String)e.getPayload());
    }
    
    public List getCardNames(){
    	//TODO How do we order this? We need an ordered map!
    	return new ArrayList(panels.keySet());
    }
    public Container getContainer(String viewName) {
        return (Container)panels.get(viewName);
    }

}
